﻿using UnityEngine;
using System.Collections;

public class RangedWeapon : MonoBehaviour, IWeapon {

	public int AmmoAmount;
	public int ClipSize;
	public float Cooldown;
	public int ShotsBeforeCooldown;
	public int Damage;
	public Transform useBullet;

	private int resetReloadAmmoAmountValue;
	private float resetCooldownValue;
	private int resetShotsBeforeCooldownValue;
	private bool cooldownEnded;

	protected SpriteRenderer spriteRenderer;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () 
	{


	}
	
	public virtual void InitWeapon()
	{
		Debug.Log("INIT RANGED");
		spriteRenderer = this.GetComponent<SpriteRenderer>();
		resetCooldownValue = Cooldown;
		resetReloadAmmoAmountValue = AmmoAmount;
		resetShotsBeforeCooldownValue = ShotsBeforeCooldown;
		cooldownEnded = true;
	}

	public virtual void UpdateWeapon()
	{
		if (!cooldownEnded)
		{
			Debug.Log("COOLDOWN : " + Cooldown);

			Cooldown -= Time.deltaTime;
			if (Cooldown <= 0)
			{
				Debug.Log("COOLDOWN RESETED");

				cooldownEnded = true;
				Cooldown = resetCooldownValue;
				ShotsBeforeCooldown = resetShotsBeforeCooldownValue;
			}
		}
	}


	public void DoWeaponAttack(Vector2 direction)
	{
		if (AllowedToShoot())
		{
			if (ShotsBeforeCooldown > 0)
			{
				OnAttack(direction);
				AmmoAmount--;
				ShotsBeforeCooldown--;
			}
			else
			{
				//start cooldown
				cooldownEnded = false;
			}
		}
	}
	
	public void Reload()
	{
		AmmoAmount = resetReloadAmmoAmountValue;

		OnReload();
	}

	public SpriteRenderer getWeaponSprite()
	{
		return spriteRenderer;
	}

	bool AllowedToShoot()
	{
		return AmmoAmount > 0 && cooldownEnded;
	}

	public virtual void OnAttack(Vector2 direction)
	{

	}

	public virtual void OnReload()
	{

	}
	
}
